#include "dashboard.h"
#include "ui_dashboard.h"
#include "global_var.h"
#include "groupdetaildialog.h"
#include "creategroupdialog.h"
#include "taskdetaildialog.h"
#include "createtaskdialog.h"
#include "utils.h"

#include <QStandardItemModel>
#include <QStringList>
#include <QDebug>

Dashboard::Dashboard(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dashboard)
{
    ui->setupUi(this);

    ui->tbv_group->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tbv_task->setEditTriggers(QAbstractItemView::NoEditTriggers);

    ui_init();
}

Dashboard::~Dashboard()
{
    delete ui;
}

void Dashboard::reload()
{
    ui_init();
}

void Dashboard::on_tbv_group_doubleClicked(const QModelIndex &index)
{
    QAbstractItemModel* model =  ui->tbv_group->model();
    QString group_name = model->data(model->index(index.row(), 0)).toString();
    Group group = get_group_by_name(group_name, current_groups);

    GroupDetailDialog* dialog = new GroupDetailDialog(group, this);
    dialog->show();
}

void Dashboard::on_tbv_task_doubleClicked(const QModelIndex &index)
{
    QAbstractItemModel* model =  ui->tbv_task->model();
    QString task_name = model->data(model->index(index.row(), 0)).toString();
    Task task = get_task_by_name(task_name, current_tasks);

    TaskDetailDialog* dialog = new TaskDetailDialog(task);
    dialog->show();
}


void Dashboard::ui_init()
{
    QStandardItem* item = NULL;

    // init group table view
    QStandardItemModel* group_model = new QStandardItemModel();
    QStringList group_headers = tr("组名,成员数,管理员").simplified().split(",");
    group_model->setHorizontalHeaderLabels(group_headers);

    for(int i=0; i < current_groups.size(); i++) {
        item = new QStandardItem(current_groups[i].getGroup_name());
        group_model->setItem(i, 0, item);

        item = new QStandardItem(QString("%1").arg(current_groups[i].get_member_size()));
        group_model->setItem(i, 1, item);

        item = new QStandardItem(current_groups[i].getGroup_manager());
        group_model->setItem(i, 2, item);
    }
    ui->tbv_group->setModel(group_model);
    ui->tbv_group->show();

    // init task table view
    QStandardItemModel* task_model = new QStandardItemModel();
    QStringList task_header = tr("任务名,所有者,结束时间,状态").simplified().split(",");
    task_model->setHorizontalHeaderLabels(task_header);

    for(int i=0; i < current_tasks.size(); i++) {
        item = new QStandardItem(current_tasks[i].getTask_name());
        task_model->setItem(i, 0, item);

        item = new QStandardItem(current_tasks[i].getTask_owner());
        task_model->setItem(i, 1, item);

        item = new QStandardItem(current_tasks[i].getEnd_time().toString(tr("yyyy-MM-dd hh:mm:ss")));
        task_model->setItem(i, 2, item);

        item = new QStandardItem(current_tasks[i].getTask_state() == ACTIVE ? "ACTIVE" : "COMPLETED");
        task_model->setItem(i, 3, item);
    }
    ui->tbv_task->setModel(task_model);
    ui->tbv_task->show();

    // init labels
    ui->lbl_uid->setText(current_user.getId());

    QString role_str;
    if(current_user.getRole() == MANAGER) {
        role_str = "manager";
    }
    else if(current_user.getRole() == TEACHER) {
        role_str = "teacher";
    }
    else {
        role_str = "student";
    }
    ui->lbl_role->setText(role_str);

    // set button
    if(current_user.getRole() == STUDENT) {
        ui->btn_add_group->setEnabled(false);
        ui->btn_add_task->setEnabled(false);
    }
}

void Dashboard::on_btn_add_group_clicked()
{
    CreateGroupDialog* dialog = new CreateGroupDialog(this);
    dialog->show();
}

void Dashboard::on_btn_add_task_clicked()
{
    CreateTaskDialog* dialog = new CreateTaskDialog(this);
    dialog->show();
}

void Dashboard::on_btn_exit_clicked()
{
    write_json_back();
    this->close();
}
